package com.lovo.test.ATM;

import com.lovo.test.mysql.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AtmMysq {
    public static Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ATM?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "Xiaohao123456");
            System.out.println("con:" + con);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    //增删改连接关闭
    public static void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    public static void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    /**
     * 修改金额
     * @param id  要修改的用户id
     * @param money  要修改的金额数量
     */
    public static void insert(int id ,int money ) {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String sql = "update t_user set u_moneys="+money+" where u_id='"+id+"'";
            statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    //修改密码
    public static void modifyPwd(int id ,String pwd ) {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String sql = "update t_user set u_pwd='"+pwd+"' where u_id='"+id+"'";
            statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }


    //查询
    public static ArrayList query() {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList<User> userList = new ArrayList();
        try {
            statement = con.createStatement();
            String sql = "select * from t_user";
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                User users = new User();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPassWord(rs.getString("u_pwd"));
                users.setMoney(rs.getInt("u_moneys"));
                userList.add(users);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userList;
    }
}
